const App = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: "",
        info: {},
        imgurl: "",
        orderid: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        this.setData({
            id: options.id,
            orderid: options.orderid,
        })

        this.getdata();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    getdata: function () {
        var that = this;
        var id = this.data.id;
        App._post_form('wxapi/v2/renwu/info', {
            id: id
        }, function (result) {
            console.log("config", result);
            that.setData({
                info: result.data,

            })
        });
    },
    back() {
        wx.navigateBack({
            delta: 1
        })
    },

    start() {
        var appid = this.data.info.appid;
        var path = this.data.info.url;

        console.log("??",appid,path);

        wx.navigateToMiniProgram({
            appId: appid,
            path: path,
            extraData: {
                foo: 'bar'
            },
            success: res => {


            },
            fail: error => {

            },
        })
    },
    upload() {
        var that = this;
        var imgs = this.data.imgs;

        wx.chooseMedia({
            count: 1, // 默认9
            mediaType: ['image'],
            sizeType: ['compressed'],
            sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                console.log("re!!!!!!!!!!!!!!!", res.tempFiles);
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFiles;
                // console.log(imgs);
                that.setData({
                    imgs: tempFilePaths
                });
                for (var i = 0; i < tempFilePaths.length; i++) {
                    console.log("哈哈")
                    that.uplodafile(tempFilePaths[i].tempFilePath);
                }


            }
        });
    },
    uplodafile(filePath) {
        var that = this;
        wx.uploadFile({
            url: App.api_root + 'upload/file3',
            filePath: filePath,
            name: 'imgFile',
            success: function (res) {

                console.log("data", JSON.parse(res.data).code)
                if (JSON.parse(res.data).code == 200) {

                    console.log("ok", JSON.parse(res.data).pic);
                    that.setData({
                        imgurl: JSON.parse(res.data).pic,
                    })
                }
            }
        })
    },
    add() {

        if (this.data.imgurl == "") {
            App.showError("请上传截图", null);
            return;
        }

        var that = this;
        var id = this.data.id;
        var token = wx.getStorageSync('token');
        App._post_form('wxapi/v2/orders/update', {
            renwuid: id,
            token: token,
            imgurl: this.data.imgurl,
            orderid: this.data.orderid
        }, function (result) {
            console.log("config", result);

            if (result.status == 200) {
                setTimeout(() => {
                    App.showSuccess("参与成功");
                }, 500);
                setTimeout(() => {
                    that.back();
                }, 2000);

            } else {
                App.showError(result.message, null);
            }

        });
    },
    fangqi(e) {
        var that = this;
        var orderid = this.data.orderid;
        wx.showModal({
            title: '系统提示',
            content: '确定要放弃吗',
            success(res) {
                if (res.confirm) {

                    var token = wx.getStorageSync('token');
                    App._post_form('wxapi/orders/del', {
                        token: token,
                        orderid: orderid
                    }, function (result) {
                        setTimeout(() => {
                            App.showSuccess("操作成功");
                        }, 500);
                        setTimeout(() => {
                            that.back();
                        }, 1000);

                    });


                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })


    },
})